#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

using ll = long long;
int main()
{
    priority_queue<ll, vector<ll>, greater<ll>> heap;
    int n;
    cin >> n;
    for(int i = 0; i < n;i++)
    {
        ll a;
        cin >> a;
        heap.push(a);
    }
    long long ret = 0;
    while(heap.size() > 1)
    {
        ll minimum = heap.top();
        heap.pop();
        ll nextmin = heap.top();
        heap.pop();
        long long cur = minimum + nextmin;
        heap.push(cur);
        ret += cur;
    }
    cout << ret << endl;
    return 0;
}